package com.example.lms.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lms.entity.Goods;
import com.example.lms.entity.ShipmentRecords;
import com.example.lms.mapper.GoodsMapper;
import com.example.lms.mapper.ShipmentRecordsMapper;
import com.example.lms.service.GoodsService;
import com.example.lms.vo.D;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @module
 * @author：DUOLUONIANDAI
 * @DATA：2024/05/11
 * @Title：
 */
@Service
public class GoodsServiceImp extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Override
    public D addGoods(Goods goods) {
        goods.setId(UUID.randomUUID().toString());
        D build = D.builder().build();

        if (save(goods)) {
            build.inputVal("更新的goods数据为：", build);

        } else {
            build.inputVal("数据库异常", build);
        }

        return build;
    }

    @Resource
    ShipmentRecordsMapper shipmentRecordsMapper;

    @Override
    public D delGoods(String id) {
        D build = D.builder().build();
        Goods id2 = getOne(new QueryWrapper<Goods>().eq("id", id));
        if (ObjectUtils.isEmpty(id2)){
            build.inputVal("物品不存在",id);
            return build;
        }

        boolean id1 = remove(new QueryWrapper<Goods>().eq("id", id));

        if (id1){
            List<ShipmentRecords> goods_id = shipmentRecordsMapper.selectList(new QueryWrapper<ShipmentRecords>().eq("goods_id", id));
            build.inputVal("删除成功！",id2);
            build.inputVal("引用的商品的有：",goods_id);
        }else {
            build.inputVal("删除失败！",id2);
        }
        return build;
    }

    @Override
    public D uploadGoods(Goods goods) {
        Goods id = getOne(new QueryWrapper<Goods>().eq("id", goods.getId()));

        D build = D.builder().build();
        if (ObjectUtils.isEmpty(id)){
            build.inputVal("用户商品不存在",goods);
            return build;
        }
        if (ObjectUtils.isEmpty(goods.getWeight())){
            goods.setWeight(id.getWeight());
        }
        if (ObjectUtils.isEmpty(goods.getName())){
            goods.setName(id.getName());
        }
        if (ObjectUtils.isEmpty(goods.getVolume())){
            goods.setVolume(id.getVolume());
        }
        boolean b = updateById(goods);

        if (b){
            build.inputVal("修改成功",goods);
            build.inputVal("旧数据为：",id);
        }else {
            build.inputVal("数据库异常",goods);
            build.inputVal("旧数据为：",id);
        }
        return build;
    }

    @Override
    public D getGoods(String id) {
        Goods goods = getOne(new QueryWrapper<Goods>().eq("id",id));
        D build = D.builder().build();
        if (ObjectUtils.isEmpty(goods)){
            build.inputVal("货物不存在",id);
        }else {
            build.inputVal("查询成功！",goods);
        }
        return build;
    }
}
